<template>
  <view>
    <!-- 搜索框 -->
    <view class="search-box">
      <!-- 使用 uni-ui 提供的搜索组件 -->
      <uni-search-bar
        cancelButton="none"
        :radius="100"
        v-model="keyword"
        @input="inputHandler"
      ></uni-search-bar>
    </view>
    <!-- 搜索建议列表 -->
    <view class="sugg-list" v-if="sugguestions.length > 0">
      <view
        class="sugg-item"
        v-for="(item, index) in sugguestions"
        :key="index"
        @click="gotoProductDetail(item)"
      >
        <view class="goods-name">{{ item.goods_name }}</view>
        <uni-icons type="arrowright" size="16"></uni-icons>
      </view>
    </view>
    <!-- 搜索历史 -->
    <view class="history-box" v-else>
      <!-- 标题区域 -->
      <view class="history-title">
        <text>搜索历史</text>
        <uni-icons type="trash" size="17" @click="clearHistoryList"></uni-icons>
      </view>

      <!-- 历史列表 -->
      <view class="history-list">
        <uni-tag
          v-for="(item, index) in historyList"
          :key="index"
          :text="item"
          @click="gotoProductList(item)"
        ></uni-tag>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      keyword: '', // 搜索关键字
      sugguestions: [], // 搜索建议的数据数组
      historyList: [] // 搜索关键词的历史记录
    }
  },
  created() {
    this.historyList = uni.getStorageSync('historyList') || []
  },
  methods: {
    inputHandler() {
      // 防抖优化 - 延时器
      clearTimeout(this.timer)
      this.timer = setTimeout(() => {
        this.getSugguestions()
      }, 500)
    },
    // 跳转到商品列表页面
    gotoProductList(keyword) {
      uni.navigateTo({
        url: `/pages_sub1/product-list/product-list?query=${keyword}`
      })
    },
    // 清空历史数据
    clearHistoryList() {
      uni.removeStorageSync('historyList')
      this.historyList = []
    },
    // 获取搜索结果数组
    async getSugguestions() {
      if (!this.keyword.trim()) return (this.sugguestions = [])
      const {
        message,
        meta: { status }
      } = await this.$http.get('/goods/qsearch', { query: this.keyword.trim() })
      if (status !== 200) return this.$showMsg()
      this.sugguestions = message
      // 将搜索额关键字添加到搜索历史的数组中
      // push() 添加到数组的最后
      // unshift() 添加到数组的开头
      // pop() 删除最后一项
      // shift() 删除第一项
      this.updateHistoryList()
    },
    // 更新搜索历史
    updateHistoryList() {
      this.historyList.unshift(this.keyword.trim())
      // 采用set方式进行去重 & 转数组
      const list = [...new Set(this.historyList)]
      if (list.length > 5) list.pop()
      this.historyList = list
      uni.setStorageSync('historyList', list)
    },
    // 点击跳转事件
    gotoProductDetail(item) {
      // 点击列表项，跳转到产品详情页
      uni.navigateTo({
        url: `/pages_sub1/product-detail/product-detail?pid=${item.goods_id}`
      })
    }
  }
}
</script>

<style scoped lang="scss">
// 使用样式穿透：覆盖 uni-search-bar 组件的 .uni-searchbar 背景色
::v-deep .uni-searchbar {
  background-color: #c00000 !important;
}

// 搜索框吸顶
.search-box {
  position: sticky;
  top: 0;
  z-index: 999;
}
// 搜索建议列表
.sugg-list {
  padding: 0 10rpx;

  .sugg-item {
    font-size: 24rpx;
    padding: 26rpx 0;
    border-bottom: 1rpx solid #efefef;
    display: flex;
    align-items: center;
    justify-content: space-between;

    .goods-name {
      margin-right: 6rpx;

      // 溢出部分隐藏
      overflow: hidden;

      // 文字不允许换行
      white-space: nowrap;

      // 文字溢出后使用省略号表示
      text-overflow: ellipsis;
    }
  }
}
// 搜索历史
.history-box {
  padding: 0 10rpx;

  .history-title {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 80rpx;
    font-size: 26rpx;
    border-bottom: 2rpx solid #efefef;
  }

  .history-list {
    display: flex;
    flex-wrap: wrap;

    uni-tag {
      margin-top: 10rpx;
      margin-right: 10rpx;
    }
  }
}
</style>
